package general.currentdomain;

import java.util.Iterator;

public class ArrayCurrentDomain extends CurrentDomain{
	int domain;
	boolean[] current_domain;
	public ArrayCurrentDomain(int domain){
		this.domain = domain;
		current_domain = new boolean[domain];
		reset();
	}
	
	@Override
	public void insert(byte i) {
		current_domain[i] = true;
	}
	@Override
	public boolean isInDomain(byte i) {
		return current_domain[i];
	}
	@Override
	public void remove(byte i) {
		current_domain[i] = false;
		
	}
	@Override
	public void reset() {
		for (byte i=0; i<domain; i++){
			insert(i);
		}
	}
	@Override
	public Iterator<Integer> iterator() {
		return new MyIterator();
	}
	
	private class MyIterator implements Iterator<Integer>{
		byte i = 0;
		public MyIterator() {
			while (i<domain && !isInDomain(i)){	i++; }
		}
		
		@Override
		public boolean hasNext() {
			return i<domain;
		}

		@Override
		public Integer next() {
			int tmp = i;
			i++;
			while (i<domain && !isInDomain(i)){	i++; } 
			return tmp;
		}

		@Override
		public void remove() {
			throw new UnsupportedOperationException("remove() unsupported for ArrayCurrentDomain");
		}
	}
	
	@Override
	public String toString() {
		String str = "";
		str+=("[");
		for (int i=0; i<domain-1; i++){
			str+=((current_domain[i] ? "T" : "F") +", ");
		}
		str+=((current_domain[domain-1] ? "T" : "F")+"]");
		return str;
	}
}
